package com.auth.manager.security.auth;

import com.auth.manager.common.resp.Result;
import com.auth.manager.utils.ResponseUtil;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @version 1.0.0
 * @Date: 2022/4/27 16:52
 * @Author ZhuYouBin
 * @Description [用户授权],主要作用是: 当用户没有访问url的权限时候, 会进入到这个类里面
 */
@Component
public class AuthDeniedHandler implements AccessDeniedHandler {

    // 这个方法就是处理权限不足的异常
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException exception) throws IOException, ServletException {
        // 设置响应状态码
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        ResponseUtil.response(response, Result.failed(exception.getMessage()));
    }

}
